﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Honestysoft.Financing.Model
{
    public partial class BD_Contract
    {
        /// <summary>
        /// 项目代码
        /// </summary>
        public string ProjectCode { get; set; }
        /// <summary>
        /// 项目名称
        /// </summary>
        public string ProjectName { get; set; }
        /// <summary>
        /// 建设单位
        /// </summary>
        public string Owner { get; set; }
        /// <summary>
        /// 项目单位
        /// </summary>
        public string Owner971 { get; set; }
        /// <summary>
        /// 建设单位名称
        /// </summary>
        public string OwnerName { get; set; }
        /// <summary>
        /// 项目单位名称
        /// </summary>
        public string Owner971Name { get; set; }
        /// <summary>
        /// 施工单位名称
        /// </summary>
        public string ContractorName { get; set; }
        /// <summary>
        /// 委托收款单位名称
        /// </summary>
        public string DBankCompanyName { get; set; }

        /// <summary>
        /// 变更后金额
        /// </summary>
        public decimal CurChangePrice { get; set; }
        /// <summary>
        /// 是否有变更记录
        /// </summary>
        public bool HasChangeRecord { get; set; }
        /// <summary>
        /// 最后一次的变更日期
        /// </summary>
        public DateTime LastChangeDate { get; set; }

        /// <summary>
        /// 刷新最后变更记录
        /// </summary>
        public void RefreshChange()
        {
            if (ContractChanges != null && ContractChanges.Count > 0)
            {
                DateTime maxChangeDate = DateTime.MinValue, maxCreateTime = DateTime.MinValue;
                foreach (BD_ContractChange item in ContractChanges)
                {
                    if (DateTime.Compare(item.ChangeDate.Value, maxChangeDate) > 0)
                    {
                        maxChangeDate = item.ChangeDate.Value;
                    }
                }
                List<BD_ContractChange> lsChange = ContractChanges.FindAll((BD_ContractChange find) => DateTime.Compare(find.ChangeDate.Value, maxChangeDate) == 0);
                foreach (BD_ContractChange item in lsChange)
                {
                    if (DateTime.Compare(item.CreateTime, maxCreateTime) > 0)
                    {
                        maxCreateTime = item.CreateTime;
                    }
                }
                BD_ContractChange curChange = lsChange.Find((BD_ContractChange find) => DateTime.Compare(find.CreateTime, maxCreateTime) == 0);
                CurChangePrice = curChange.ChangeMoney;
                LastChangeDate = curChange.ChangeDate.Value;
                ChangePrice = CurChangePrice;
                HasChangeRecord = true;
                dataState = EnumItem.DataState.Modify;
            }
            else
            {
                CurChangePrice = ChangePrice.Value;
                LastChangeDate = CreateTime;
                HasChangeRecord = false;
            }
        }

       
        /// <summary>
        /// 合同变更信息
        /// </summary>
        public List<BD_ContractChange> ContractChanges
        {
            get;
            set;
        }
        /// <summary>
        /// 合同项目委托代建单位
        /// </summary>
        public List<BD_Contract_ProjectDOwner> ProjectDOwners { get; set; }

        private EnumItem.DataState _dataState = EnumItem.DataState.Unchange;
        public EnumItem.DataState dataState
        {
            get { return _dataState; }
            set { _dataState = value; }
        }
    }
}
